﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.ServiceModel;
using System.IO;

namespace MReviewerClient.Providers
{
    /// <summary>
    /// The class is used for storage the information about service in file
    /// </summary>
    public class ServiceInfoProvider
    {
        #region Properties & Fields

        /// <summary>
        /// The name of Service will be hosted
        /// </summary>
        public string ServiceName { get; set; }

        /// <summary>
        /// The port which is corresponding with service
        /// </summary>
        public string Port { get; set; }

        /// <summary>
        /// The URL address lead to service will be hosted
        /// </summary>
        public string ServiceURL
        {
            get
            {
                return string.Format("http://{0}:{1}/{2}", this.ServiceName, this.Port, SERVICE_PATH);
            }
        }

        /// <summary>
        /// The instance of FileManager
        /// </summary>
        private static ServiceInfoProvider instance;

        /// <summary>
        /// The lock object for synchronization
        /// </summary>
        private static object syncLock = new object();

        /// <summary>
        /// The singleton instance of FileManager
        /// </summary>
        public static ServiceInfoProvider Instance
        {
            get
            {
                lock (syncLock)
                {
                    if (instance == null)
                        instance = new ServiceInfoProvider();
                    return instance;
                }
            }
        }

        /// <summary>
        /// Is service info invalid
        /// </summary>
        public bool IsServiceInvalid
        {
            get
            {
                return (ServiceInfoProvider.Instance.ServiceName == null || ServiceInfoProvider.Instance.Port == null ||
                ServiceInfoProvider.Instance.ServiceName.Equals(string.Empty) || ServiceInfoProvider.Instance.Port.Equals(string.Empty));
            }
        }
        /// <summary>
        /// The service path
        /// </summary>
        public const string SERVICE_PATH = "SvcMReviewer.svc";

        /// <summary>
        /// The name of file which is used to store service information
        /// </summary>
        private const string FILENAME = "Sources.res";

        #endregion

        #region Methods

        /// <summary>
        /// Storage the information about service in file
        /// </summary>
        /// <param name="serviceName">The name of Service will be hosted</param>
        /// <param name="port">The port which is corresponding with service</param>
        /// <returns></returns>
        public string StoreServiceInfo(string serviceName, string port)
        {
            try
            {
                var file = IsolatedStorageFile.GetUserStoreForApplication();

                if (file.FileExists(FILENAME))
                    file.DeleteFile(FILENAME);

                var fs = file.CreateFile(FILENAME);
                var writer = new StreamWriter(fs);

                writer.WriteLine(serviceName);
                writer.WriteLine(port);

                // Update service information
                this.ServiceName = serviceName;
                this.Port = port;

                writer.Close();
                fs.Close();
                fs.Dispose();

                return string.Empty;
            }
            catch (Exception ex)
            {
                if(ex.InnerException != null)
                    return ex.InnerException.Message;
                return ex.Message;
            }
        }

        /// <summary>
        /// Read the Server Name and Port from the isolated saved earlier
        /// </summary>
        /// <param name="serviceURL">Service address</param>
        public string ReadServiceInfo()
        {
            try
            {
                var file = IsolatedStorageFile.GetUserStoreForApplication();
                var strArr = file.GetFileNames();

                if (file.FileExists(FILENAME))
                {
                    var fs = file.OpenFile(FILENAME, FileMode.Open);
                    var reader = new StreamReader(fs);

                    // Read service information
                    this.ServiceName = reader.ReadLine();
                    this.Port = reader.ReadLine();

                    reader.Close();
                    fs.Close();
                    fs.Dispose();
                }

                return string.Empty;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    return ex.InnerException.Message;
                return ex.Message;
            }
        }

        public string DeleteFile()
        {
            try
            {
                var file = IsolatedStorageFile.GetUserStoreForApplication();

                if (file.FileExists(FILENAME))
                    file.DeleteFile(FILENAME);

                return string.Empty;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    return ex.InnerException.Message;
                return ex.Message;
            }
        }

        #endregion
    }
}
